package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCommentReply;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentReplyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 攻略点评的实现类
 */
@Service
public class StrategyCommentReplyServiceImpl implements IStrategyCommentReplyService {
    @Autowired
    private StrategyCommentReplyMapper strategyCommentReplyMapper;

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyCommentReply> list = strategyCommentReplyMapper.selectForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void changeReplyState(Long id, Integer state) {
        if(state == 0){
            state = 1;
        }else {
            state = 0;
        }
        strategyCommentReplyMapper.changeReplyState(id,state);
    }

    @Override
    public void saveReply(StrategyCommentReply entity) {
        //设置评论时间
        entity.setCreateTime(new Date());
        //设置点评的作者
        Long strategyCommentId = entity.getStrategyCommentId();
        User author = strategyCommentMapper.selectByPrimaryKey(strategyCommentId).getUser();
        entity.setAuthor(author);
        //保存
        strategyCommentReplyMapper.insert(entity);
        //保存后,查出评论总数,设置到点评的评论数中
        int count = strategyCommentReplyMapper.getCountByStrategyCommentId(strategyCommentId);
        strategyCommentMapper.updateReplyCount(strategyCommentId,count);

    }

    @Override
    public int getCountByStrategyCommentId(Long strategyCommentId) {
        return strategyCommentReplyMapper.getCountByStrategyCommentId(strategyCommentId);
    }

}
